package com.cmj.controller;

import com.cmj.domain.AppHttpCodeEnum;
import com.cmj.domain.ResponseResult;
import com.cmj.domain.entity.User;
import com.cmj.exception.SystemException;
import com.cmj.service.BlogLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 这是注释
 */
@RestController
public class BlogLoginController {
    @Autowired
    private BlogLoginService blogLoginService;

    /***
     * 登录
     * @param user 用户信息
     * @return 登录token和信息
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        //没有传入用户名
        if (!StringUtils.hasText(user.getUserName())){
            throw  new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return blogLoginService.login(user);
    }

    /***
     * 退出登录
     * @return 操作结果
     */
    @PostMapping("/logout")
    public ResponseResult logout(){
        return blogLoginService.logout();
    }
}
